﻿using System.Collections.Generic;
using System.Web.Script.Serialization;
using Taype.Trading.BTC.Utilities;
using System;

namespace Taype.Trading.BTC.MtGoxAPIClient.JSONObjects
{
    /// <summary>
    ///  {
    ///  "high": {"value":"5.70653","value_int":"570653","display":"$5.70653","currency":"USD"},
    ///  "low": {"value":"5.4145","value_int":"541450","display":"$5.41450","currency":"USD"},
    ///  "avg": {"value":"5.561119626","value_int":"556112","display":"$5.56112","currency":"USD"},
    ///  "vwap": {"value":"5.610480461","value_int":"561048","display":"$5.61048","currency":"USD"},
    ///  "vol":
    ///  {
    ///   "value":"55829.58960346",
    ///   "value_int":"5582958960346",
    ///   "display":"55,829.58960346\u00a0BTC",
    ///   "currency":"BTC"
    ///  },
    ///  "last_local":{"value":"5.5594","value_int":"555940","display":"$5.55940","currency":"USD"},
    ///  "last_orig":{"value":"5.5594","value_int":"555940","display":"$5.55940","currency":"USD"},
    ///  "last":{"value":"5.5594","value_int":"555940","display":"$5.55940","currency":"USD"},
    ///  "buy":{"value":"5.53587","value_int":"553587","display":"$5.53587","currency":"USD"},
    ///  "sell":{"value":"5.56031","value_int":"556031","display":"$5.56031","currency":"USD"}
    ///  }
    /// </summary>
    public class MtGoxMultiCurrencyTickerItem
    {

        public MtGoxMultiCurrencyTickerItemDetail high { get; set; }

        public MtGoxMultiCurrencyTickerItemDetail low { get; set; }

        public MtGoxMultiCurrencyTickerItemDetail avg { get; set; }

        public MtGoxMultiCurrencyTickerItemDetail vwap { get; set; }

        public MtGoxMultiCurrencyTickerItemDetail vol { get; set; }

        public MtGoxMultiCurrencyTickerItemDetail last { get; set; }

        public MtGoxMultiCurrencyTickerItemDetail buy { get; set; }

        public MtGoxMultiCurrencyTickerItemDetail sell { get; set; }

        public MtGoxMultiCurrencyTickerItemDetail last_all { get; set; }

        public MtGoxMultiCurrencyTickerItemDetail last_local { get; set; }

        public MtGoxMultiCurrencyTickerItem()
        { }

        public MtGoxMultiCurrencyTickerItem(MtGoxMultiCurrencyTickerItemDetail high, MtGoxMultiCurrencyTickerItemDetail low, MtGoxMultiCurrencyTickerItemDetail avg, MtGoxMultiCurrencyTickerItemDetail vwap, MtGoxMultiCurrencyTickerItemDetail vol, MtGoxMultiCurrencyTickerItemDetail last_all, MtGoxMultiCurrencyTickerItemDetail last_local, MtGoxMultiCurrencyTickerItemDetail last, MtGoxMultiCurrencyTickerItemDetail buy, MtGoxMultiCurrencyTickerItemDetail sell)
        {
            this.avg = avg;
            this.buy = buy;
            this.high = high;
            this.last = last;
            this.low = low;
            this.sell = sell;
            this.vol = vol;
            this.vwap = vwap;
            this.last_all = last_all;
            this.last_local = last_local;
        }

        public static MtGoxMultiCurrencyTickerItem getObjects(string jsonDataStr)
        {
            string json = jsonDataStr;
            // we have a language issue here... the json string returned by this method will have a key called "return" which we cannot use as an type name in our code since it is a reserved word in C#
            // let's munge the string so that return is represented as "ret"
            json = jsonDataStr.Replace("return", "ret");
            var serializer = new JavaScriptSerializer();
            serializer.RegisterConverters(new[] { new DynamicJsonConverter() });
            dynamic obj = serializer.Deserialize(json, typeof(object));
            MtGoxMultiCurrencyTickerItem tk = new MtGoxMultiCurrencyTickerItem();
            if(json.Contains("ret"))
            {
                tk.avg = MtGoxMultiCurrencyTickerItemDetail.getObjects(obj.ret.avg.ToString());
                tk.buy = MtGoxMultiCurrencyTickerItemDetail.getObjects(obj.ret.buy.ToString());
                tk.high = MtGoxMultiCurrencyTickerItemDetail.getObjects(obj.ret.high.ToString());
                tk.last = MtGoxMultiCurrencyTickerItemDetail.getObjects(obj.ret.last.ToString());
                tk.low = MtGoxMultiCurrencyTickerItemDetail.getObjects(obj.ret.low.ToString());
                tk.sell = MtGoxMultiCurrencyTickerItemDetail.getObjects(obj.ret.sell.ToString());
                tk.vol = MtGoxMultiCurrencyTickerItemDetail.getObjects(obj.ret.vol.ToString());
                tk.vwap = MtGoxMultiCurrencyTickerItemDetail.getObjects(obj.ret.vwap.ToString());
                tk.last_all = MtGoxMultiCurrencyTickerItemDetail.getObjects(obj.ret.last_all.ToString());
                tk.last_local = MtGoxMultiCurrencyTickerItemDetail.getObjects(obj.ret.last_local.ToString());
            }
            else
            {
                tk.avg = MtGoxMultiCurrencyTickerItemDetail.getObjects(obj.avg.ToString());
                tk.buy = MtGoxMultiCurrencyTickerItemDetail.getObjects(obj.buy.ToString());
                tk.high = MtGoxMultiCurrencyTickerItemDetail.getObjects(obj.high.ToString());
                tk.last = MtGoxMultiCurrencyTickerItemDetail.getObjects(obj.last.ToString());
                tk.low = MtGoxMultiCurrencyTickerItemDetail.getObjects(obj.low.ToString());
                tk.sell = MtGoxMultiCurrencyTickerItemDetail.getObjects(obj.sell.ToString());
                tk.vol = MtGoxMultiCurrencyTickerItemDetail.getObjects(obj.vol.ToString());
                tk.vwap = MtGoxMultiCurrencyTickerItemDetail.getObjects(obj.vwap.ToString());
                tk.last_all = MtGoxMultiCurrencyTickerItemDetail.getObjects(obj.last_all.ToString());
                tk.last_local = MtGoxMultiCurrencyTickerItemDetail.getObjects(obj.last_local.ToString());
            }
            return tk;
        }
            
    }
}
